# import necessary packages
import argparse
import cv2 as cv

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="The Path To Image")
args = vars(ap.parse_args())

# load the image,convert it to grayscale,and blur it slightly
image = cv.imread(args["image"])
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (7, 7), 0)
cv.imshow("Image", image)

# apply basic thresholding -- the first parameter is the image
# we want to threshold,the second value is our threshold check
# if a pixels value is greater than our threshold(in this case
# 200),we set it to be black,otherwise it is white.
# 其实就是这么回事,当是cv.2.ThRES_BINARY_INV的时候
#  dst(x,y) = 0 if src(x,y) > T else maxVal

# 大于200的变成黑色,小于等于200的变成255
(T, thresInv) = cv.threshold(blurred, 200, 255, cv.THRESH_BINARY_INV)
cv.imshow("Threshold Binary Inverse", thresInv)

# using normal thresholding (rather than inverse thresholding),
# we can change the last argument in the function to amke the coins
# black rather than white
# 小于200的像素置位0,大于200的像素置位255
(T, thres) = cv.threshold(blurred, 200, 255, cv.THRESH_BINARY)
cv.imshow("Threshold Binary", thres)

# finally,we can visualize only the masked regions in the image
cv.imshow("Output", cv.bitwise_and(image, image, mask=thresInv))

